package cn.wolfcode.luowowo.serach.service.impl;

import cn.wolfcode.luowowo.search.repository.IUserInfoTemplateRepository;
import cn.wolfcode.luowowo.search.service.IUserInfoSearchService;
import cn.wolfcode.luowowo.search.template.UserInfoTemplate;
import com.alibaba.dubbo.config.annotation.Service;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;

@Service
public class UserInfoSearchServiceImpl implements IUserInfoSearchService {

    @Autowired
    private IUserInfoTemplateRepository dao;

    @Autowired
    private ElasticsearchTemplate template;

    @Autowired
    private TransportClient client;

    public void createIndex(){
        template.createIndex(UserInfoTemplate.class);
        template.putMapping(UserInfoTemplate.class);
    }

    public void save(UserInfoTemplate template) {
        dao.save(template);
    }

    public Page<UserInfoTemplate> query(String name) {
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        boolQuery.must(QueryBuilders.termQuery("destName",name));
        return dao.search(boolQuery, PageRequest.of(0, 5));
    }
}
